import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _isEmpty from "lodash/isEmpty";
var _excluded = ["form"],
    _excluded2 = ["title", "visible", "onCancel", "onOk", "dataSource", "pagination", "loading", "defaultSelectedRow"];

/**
 * 菜单管理 - 上级目录
 * TODO: 没有给宽度 会被撑开
 */
import React, { PureComponent } from 'react';
import { Modal, Table, Form, Button, Input } from 'antd';
import intl from 'utils/intl';
import { tableScrollWidth } from 'utils/utils';
var FormItem = Form.Item;
var FormContext = React.createContext();
var viewMessagePrompt = 'ciam.tenantMenu.view.message';
var modelPrompt = 'ciam.tenantMenu.model.tenantMenu';
var commonPrompt = 'cpaas.common';

var QueryForm = function QueryForm(_ref) {
  var form = _ref.form,
      props = _objectWithoutProperties(_ref, _excluded);

  return /*#__PURE__*/React.createElement(FormContext.Provider, {
    value: form
  }, /*#__PURE__*/React.createElement(Form, props));
};

var WrapperQueryForm = Form.create({
  fieldNameProp: null
})(QueryForm);

var DirModal = /*#__PURE__*/function (_PureComponent) {
  _inherits(DirModal, _PureComponent);

  var _super = _createSuper(DirModal);

  function DirModal() {
    var _this;

    _classCallCheck(this, DirModal);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      selectedRow: _this.props.defaultSelectedRow || {}
    };
    _this.onTableChange = void 0;
    _this.defaultRowkey = 'id';
    return _this;
  }

  _createClass(DirModal, [{
    key: "onSelect",
    value: function onSelect(selectedRow) {
      this.setState({
        selectedRow: selectedRow
      });
    }
  }, {
    key: "handleRowClick",
    value: function handleRowClick(record) {
      this.setState({
        selectedRow: record
      });
    }
  }, {
    key: "ok",
    value: function ok() {
      var _this$props = this.props,
          _this$props$onOk = _this$props.onOk,
          onOk = _this$props$onOk === void 0 ? function (e) {
        return e;
      } : _this$props$onOk,
          _this$props$onCancel = _this$props.onCancel,
          onCancel = _this$props$onCancel === void 0 ? function (e) {
        return e;
      } : _this$props$onCancel,
          _this$props$defaultSe = _this$props.defaultSelectedRow,
          defaultSelectedRow = _this$props$defaultSe === void 0 ? {} : _this$props$defaultSe;
      var selectedRow = this.state.selectedRow;
      this.setState({
        selectedRow: {}
      });
      onOk(_isEmpty(selectedRow) ? defaultSelectedRow : selectedRow);
      onCancel();
    }
  }, {
    key: "cancel",
    value: function cancel() {
      var _this$props$onCancel2 = this.props.onCancel,
          onCancel = _this$props$onCancel2 === void 0 ? function (e) {
        return e;
      } : _this$props$onCancel2;
      onCancel();
    }
  }, {
    key: "getWrapperForm",
    value: function getWrapperForm() {
      var _this2 = this;

      var _this$props$handleSea = this.props.handleSearch,
          handleSearch = _this$props$handleSea === void 0 ? function (e) {
        return e;
      } : _this$props$handleSea;
      return /*#__PURE__*/React.createElement(WrapperQueryForm, {
        layout: "inline",
        style: {
          marginBottom: 15
        }
      }, /*#__PURE__*/React.createElement(FormContext.Consumer, null, function (form) {
        var _form$getFieldDecorat = form.getFieldDecorator,
            getFieldDecorator = _form$getFieldDecorat === void 0 ? function (e) {
          return e;
        } : _form$getFieldDecorat,
            _form$resetFields = form.resetFields,
            resetFields = _form$resetFields === void 0 ? function (e) {
          return e;
        } : _form$resetFields,
            getFieldsValue = form.getFieldsValue;

        var search = function search() {
          handleSearch(_objectSpread({
            page: 0,
            size: 10
          }, getFieldsValue()));
        };

        _this2.onTableChange = function () {
          var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
          var _pagination$current = pagination.current,
              current = _pagination$current === void 0 ? 1 : _pagination$current,
              _pagination$pageSize = pagination.pageSize,
              pageSize = _pagination$pageSize === void 0 ? 10 : _pagination$pageSize;
          handleSearch(_objectSpread({
            page: current - 1,
            size: pageSize
          }, getFieldsValue()));
        };

        return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(FormItem, {
          label: intl.get("".concat(modelPrompt, ".dirCode")).d('目录编码')
        }, getFieldDecorator('code')( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, {
          label: intl.get("".concat(modelPrompt, ".dirName")).d('目录名称')
        }, getFieldDecorator('name')( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, null, /*#__PURE__*/React.createElement(Button, {
          onClick: function onClick() {
            return resetFields();
          }
        }, intl.get("".concat(commonPrompt, ".button.reset")).d('重置'))), /*#__PURE__*/React.createElement(FormItem, null, /*#__PURE__*/React.createElement(Button, {
          type: "primary",
          icon: "search",
          htmlType: "submit",
          onClick: search
        }, intl.get("".concat(commonPrompt, ".button.search")).d('查询'))));
      }));
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _this$props2 = this.props,
          title = _this$props2.title,
          visible = _this$props2.visible,
          onCancel = _this$props2.onCancel,
          onOk = _this$props2.onOk,
          dataSource = _this$props2.dataSource,
          pagination = _this$props2.pagination,
          loading = _this$props2.loading,
          defaultSelectedRow = _this$props2.defaultSelectedRow,
          others = _objectWithoutProperties(_this$props2, _excluded2);

      var _this$state$selectedR = this.state.selectedRow,
          selectedRow = _this$state$selectedR === void 0 ? {} : _this$state$selectedR;
      var columns = [{
        title: intl.get("".concat(modelPrompt, ".dirCode")).d('目录编码'),
        dataIndex: 'code'
      }, {
        title: intl.get("".concat(modelPrompt, ".dirName")).d('目录名称'),
        dataIndex: 'name',
        width: 200
      }, {
        title: intl.get("".concat(modelPrompt, ".type")).d('类别'),
        dataIndex: 'typeMeaning',
        width: 120
      }];
      var tableProps = {
        columns: columns,
        scroll: {
          x: tableScrollWidth(columns)
        },
        rowKey: this.defaultRowkey,
        rowSelection: {
          type: 'radio',
          selectedRowKeys: [!_isEmpty(selectedRow.id) ? selectedRow.id : defaultSelectedRow.id],
          onSelect: this.onSelect.bind(this)
        },
        pagination: pagination,
        dataSource: dataSource,
        onChange: this.onTableChange,
        loading: loading,
        bordered: true,
        childrenColumnName: 'subMenus',
        onRow: function onRow(record, index) {
          return {
            onDoubleClick: function onDoubleClick() {
              return _this3.ok(record, index);
            },
            onClick: function onClick() {
              return _this3.handleRowClick(record, index);
            }
          };
        }
      };
      return /*#__PURE__*/React.createElement(Modal, Object.assign({
        title: intl.get("".concat(viewMessagePrompt, ".title.selectParentDir")).d('选择上级目录'),
        visible: visible,
        onOk: this.ok.bind(this),
        onCancel: this.cancel.bind(this),
        destroyOnClose: true,
        width: 700
      }, others), this.getWrapperForm.bind(this)(), /*#__PURE__*/React.createElement(Table, tableProps));
    }
  }]);

  return DirModal;
}(PureComponent);

export { DirModal as default };